package pub.wkq.api.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ 的配置类
 */
@Configuration
public class InitResumeMQConfig {
    //定义交换机的名称
    public static final String INIT_RESUME_EXCHANGE = "init_resume_exchange";
    //定义队列的名称
    public static final String INIT_RESUME_QUEUE = "init_resume_queue";
    //定义routingKey
    public static final String INIT_RESUME_ROUTING_KEY = "init.resume.display";


    /**
     * 创建交换机
     *
     * @return 交换机
     */
    @Bean(INIT_RESUME_EXCHANGE)
    public Exchange exchange() {
        return ExchangeBuilder.topicExchange(INIT_RESUME_EXCHANGE).durable(true).build();
    }

    /**
     * 创建队列
     *
     * @return 队列
     */
    @Bean(INIT_RESUME_QUEUE)
    public Queue queue() {
        return QueueBuilder.durable(INIT_RESUME_QUEUE).withArgument("x-max-length", 100) //队列的最大长度
                .build();
    }

    /**
     * 创建绑定关系
     *
     * @param queue    队列
     * @param exchange 交换机
     * @return 绑定关系
     */
    @Bean(INIT_RESUME_EXCHANGE + INIT_RESUME_QUEUE)
    public Binding binding(@Qualifier(INIT_RESUME_QUEUE) Queue queue, @Qualifier(INIT_RESUME_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(INIT_RESUME_ROUTING_KEY).noargs();
    }
}
